#include "debug_dialog.h"
#include "ui_debug_dialog.h"
#include "main.h"

debug_Dialog::debug_Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::debug_Dialog)
{
    ui->setupUi(this);
    ftpWin=new FtpWindow("/tmp/",this);
    connect(ftpWin,SIGNAL(download_finished(QString)),SLOT(update_finished(QString)));

    if(whole_machine_parameter.password_protect)
    {
        ui->password_checkBox->setCheckState(Qt::Checked);
    }
    else  {
        ui->password_checkBox->setCheckState(Qt::Unchecked);
       }

    strlist<< trUtf8("下水位")<<trUtf8("上水位")<<trUtf8("冷却水")<<trUtf8("压力到位")\
               <<trUtf8("前门关到位")<<trUtf8("前门开到位")<<trUtf8("前门密封")\
               <<trUtf8("后门开到位")<<trUtf8("后门关到位") <<trUtf8("后门密封");
    IOMonitor=new LedArray(10,strlist,true);
    IOMonitor->setParent(this);
    IOMonitor->setGeometry(0,40,150,400);
    mytimer=new QTimer;
    QObject:: connect( mytimer, SIGNAL(timeout()), SLOT(timeDone()) );
    QObject:: connect( ui->close_btn, SIGNAL(clicked()), SLOT(exit()) );

}

debug_Dialog::~debug_Dialog()
{
    cmd.close();
    delete ftpWin;
    delete ui;
    delete mytimer;
    delete IOMonitor;
}

void debug_Dialog::on_update_btn_clicked()
{
    BUTTON_SOUND;
    QFile::remove("/tmp/app");

  if(QFile::exists(SD_PATH))
 {

    if( Affirm("确认是否升级程序？"))
    {

       if( QFile::copy(SD_PATH,"/tmp/app") )
       {
         if(QFile::remove(EXECUTABLE_FILE) && QFile::copy("/tmp/app",EXECUTABLE_FILE))
             restart_application();
       }
       else WARN->show_text("升级失败！请重试！");
     }

  }
  else
    {
        if(Affirm("本地升级失败，是否启用网络连接？"))
         {
            ftpWin->showFullScreen();
         }

    }

}

void debug_Dialog::update_finished(QString filename)
{
   // if(Affirm("确认升级到新版本？"))
  {
    QString pathname= LOCAL_PATH +filename;
    bool remove_success=true;
    if(QFile::exists(pathname)) remove_success=QFile::remove(pathname);
    if(remove_success && QFile::copy("/tmp/"+filename, pathname))
    {
        chmod(QString(LOCAL_PATH+filename).toAscii(),0x00777);
        if(filename=="app") restart_application();
    }
   }
}

void debug_Dialog::restart_application()
{
    ::close(pwm->pwm_fd);
    STOP_ACTION
    heating_valve_OFF
    qApp->quit();
}



void debug_Dialog::on_printer_initial_clicked()
{
        BUTTON_SOUND;
        if( Affirm("确认是否初始化打印机？"))
         {
        serialprinter->tty_file.write(QByteArray::fromHex("1B09"));//开flash
        serialprinter->tty_file.write(QByteArray::fromHex("1C26"));//汉字
        serialprinter->tty_file.write(QByteArray::fromHex("1B6300"));//反向
        serialprinter->tty_file.write(QByteArray::fromHex("1B2101"));//点阵大小12*12
        serialprinter->tty_file.write(QByteArray::fromHex("1B15"));//保存
        ui->printer_initial->setText(trUtf8("打印机初始化\n已完成"));
        }
}

void debug_Dialog::on_printer_test_btn_clicked()
{
    BUTTON_SOUND;

    if( Affirm("确认是否测试打印机？"))
    { serialprinter->print_file("/qt/sterilization/doc"); }
}

void debug_Dialog::on_sterilization_default_setting_recovery_btn_clicked()
{
     BUTTON_SOUND;
    if( Affirm("确认是否初始化所有灭菌参数？") )
    {
        sterilization_default_setting_recovery(textiles_datamap,1);
        sterilization_default_setting_recovery(metals_datamap,1);
        sterilization_default_setting_recovery(bottom_datamap,2);
        sterilization_default_setting_recovery(liquid_datamap,2);
        sterilization_default_setting_recovery(sensor_adjust_datamap,3);
        sterilization_default_setting_recovery(IO_setting_datamap,4);

        whole_machine_parameter.limits_unlock=0;
        whole_machine_parameter.license=0;
        whole_machine_parameter.is_water_supply_type=0;
        whole_machine_parameter.cold_exhaust_control_type=1;
        WARN->show_text("初始化完成！");
        config_changed=true;
    }
}

void debug_Dialog::on_ts_calibrate_btn_clicked()
{
    BUTTON_SOUND;
   if( Affirm("确认是否校准触摸屏？") )
   {
       cmd.start("/home/mytslib/bin/ts_calibrate");
   }

}

void debug_Dialog::on_system_manager_btn_clicked()
{
    w.filedialog.show();
    w.showFullScreen();
}

void debug_Dialog::on_password_checkBox_stateChanged(int arg1)
{
    if(arg1==Qt::Checked)  whole_machine_parameter.password_protect=true;
    else whole_machine_parameter.password_protect=false;
    config_changed=true;

}

void debug_Dialog::on_continuous_checkBox_stateChanged(int arg1)
{
    if(arg1==Qt::Checked) continuous_mode=1;
    else   continuous_mode=0;

}
void debug_Dialog::entrance()
{
    mytimer->start(1000);
   // timeDone();
    showFullScreen();

}

void debug_Dialog::exit()
{
    mytimer->stop();

}

void debug_Dialog::timeDone()
{

    IOMonitor->set_one_value(0,bottom_water_level_isreached);
    IOMonitor->set_one_value(1,top_water_level_isreached);
    IOMonitor->set_one_value(2,water_switch_isopen);
    IOMonitor->set_one_value(3,inter_pressure_switch_isreached);
    IOMonitor->set_one_value(4,front_door_closed_in_place);
    IOMonitor->set_one_value(5,front_door_opened_in_place);
    IOMonitor->set_one_value(6,front_door_Sealing_strip_pressure_in_place);
    IOMonitor->set_one_value(7,back_door_opened_in_place);
    IOMonitor->set_one_value(8,back_door_closed_in_place);
    IOMonitor->set_one_value(9,back_door_Sealing_strip_pressure_in_place);

}

void debug_Dialog::on_debug_info_btn_clicked()
{

    QString filename = QFileDialog::getOpenFileName(this,
    trUtf8("打开文件"), "/qt" );
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
    textbrowser->setText(trUtf8(file.readAll()));
    textbrowser->show();
}

void debug_Dialog::on_debug_platform_btn_clicked()
{
   debug_dialog.show();
}

void debug_Dialog::on_test_checkBox_stateChanged(int arg1)
{
    if(arg1==Qt::Checked) test_mode=1;
    else   test_mode=0;
}
